//
//  MasterViewController.m
//  DuBai
//
//  Created by darktm on 14-2-20.
//  Copyright (c) 2014年 darktm. All rights reserved.
//

#import "MasterViewController.h"

@interface MasterViewController ()<UITabBarControllerDelegate,UITabBarDelegate>

@property(strong,atomic)UIView *prompt;

@end

@implementation MasterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.delegate = self;
    [self setNoHighlightTabBar];
    
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(15, 44, 50, 3)];
    v.backgroundColor = [UIColor brownColor];
    [self.tabBar addSubview:v];
    self.prompt = v;

    NSArray *items = self.tabBar.items;
    for (UITabBarItem *item in items) {
        
        [item setTitlePositionAdjustment:UIOffsetMake(0, -5)];
    }
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    
    NSArray *items = self.tabBar.items;
    NSInteger index = [items indexOfObject:item];
    
    CGRect frame = self.prompt.frame;
    float dur = 80.0f;
    frame.origin.x = index * dur + 15;
    [UIView animateWithDuration:.2 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
        self.prompt.frame = frame;
    } completion:NULL];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - NoHighlightTab

- (void)setSelectedIndex:(NSUInteger)index
{
    [super setSelectedIndex:index];
    [self setNoHighlightTabBar];
}

- (void)setSelectedViewController:(UIViewController *)selectedViewController
{
    [super setSelectedViewController:selectedViewController];
    [self setNoHighlightTabBar];
}

- (void)setNoHighlightTabBar
{
    NSArray * tabBarSubviews = [self.tabBar subviews];
    for(int i = [tabBarSubviews count] - 1; i >= 0; i--)
    {
        for(UIView * v in [[tabBarSubviews objectAtIndex:i] subviews])
        {
            if(v && [NSStringFromClass([v class]) isEqualToString:@"UITabBarSelectionIndicatorView"])
            {//the v is the highlight view.
                [v removeFromSuperview];
                break;
            }
        }
    }
}

@end
